package dao;

import entity.Order;
import entity.Product;
import entity.SaleStatistic;
import entity.User;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import util.MorphiaUtil;
import vo.VisitVO;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * ClassName: SaleStatisticDAO
 * Package: dao
 * Description:
 *
 * @Author LIEX
 * @create 2024/7/15 20:57
 * @Version 1.0
 */
public class SaleStatisticDAO {

    //根据year查询一年数据
    public List<SaleStatistic> findByYear(Integer year) {
        Datastore ds = MorphiaUtil.getDB();
        Query<SaleStatistic> query = ds.createQuery(SaleStatistic.class);
        List<SaleStatistic> list = query.asList();
        List<SaleStatistic> ans=new ArrayList<>();
        for (SaleStatistic saleStatistic : list) {
            if (saleStatistic.getYear().equals(year)) {
                ans.add(saleStatistic);
            }
        }
        ans.sort(Comparator.comparingInt(SaleStatistic::getMonth));
        System.out.println(ans);
        return ans;
    }
    public VisitVO findVisit(){
        VisitVO vo=new VisitVO();
        Datastore ds = MorphiaUtil.getDB();
        Query<Product> query = ds.createQuery(Product.class);
        List<Product> list = query.asList();
//        按照销售量降序排序
        list.sort((p1,p2)->{
            return (int)(-p1.getSaleCount()+p2.getSaleCount());
        });
        System.out.println(list);
        for (Product product : list) {
            product.setIdProduct(product.getId().toString());
            //热点商品Top5
            if(vo.getVisitList().size()<5){
                vo.getVisitList().add(product);
            }
            //库存低于20,加入待补货列表
            if(product.getSum()<100000){
                vo.getStorageList().add(product);
            }
            //添加商品访问量占比
            if(product.getVisitCount()<100){
                 vo.setNum1(vo.getNum1()+1);
            }else if(product.getVisitCount()<1000){
                vo.setNum2(vo.getNum2()+1);
            }else if(product.getVisitCount()<5000){
                vo.setNum3(vo.getNum3()+1);
            }else{
                vo.setNum4(vo.getNum4()+1);
            }
        }
        System.out.println(list);
        return vo;
    }
    public VisitVO getUserVisit(){
        VisitVO vo=new VisitVO();
        Datastore ds = MorphiaUtil.getDB();
        Query<Product> query = ds.createQuery(Product.class);
        Query<User> query1 = ds.createQuery(User.class);
        Query<Order> query2 = ds.createQuery(Order.class);
        long productNum = query.count();
        long userNum = query1.count();
        long saleNum = 0L;
        long saleMoney=0L;
        List<Product> list = query.asList();
        for (Product product : list) {
            saleNum+=product.getSaleCount();
        }
        List<Order> list1 = query2.asList();
        for (Order order : list1) {
            saleMoney+=order.getTotalPrice();
        }
        vo.setProductNum(productNum);
        vo.setSaleMoney(saleMoney);
        vo.setUserNum(userNum);
        vo.setSaleNum(saleNum);
        System.out.println(saleMoney);
        System.out.println(saleNum);
        return vo;
    }
}
